Conditions | 17 |
Total Lines | 76 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like helpers.js ➔ formToSky often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
50 | }); |
||
51 | |||
52 | var setLoader = function (form) { |
||
53 | var $submitButton = getSubmitButton(form); |
||
54 | if ($submitButton !== null) { |
||
55 | var initialButton = $submitButton.outerHTML; |
||
56 | $submitButton.innerHTML = ''; |
||
57 | $submitButton.outerHTML = '<div style="width:1em;height:1em;border: 3px solid #222;border-top-color: #fff;border-radius: 50%; animation: 1s spin linear infinite;"></div><style>@keyframes spin {from{transform:rotate(0deg)}to{transform:rotate(360deg)}}</style>'; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | var sendForm = function(form) { |
||
62 | setLoader(form); |
||
63 | |||
64 | var formData = new FormData(form.srcElement); |
||
65 | fetch(form.srcElement.action, { |
||
66 | method: "POST", |
||
67 | body: formData, |
||
68 | credentials: "same-origin" |
||
69 | }) |
||
70 | .then(function(response) { |
||
71 | return response.text(); |
||
72 | }) |
||
73 | .then(function(body) { |
||
74 | form.srcElement.outerHTML = body; |
||
75 | |||
76 | document.dispatchEvent(new Event("DOMChanged")); |
||
77 | }) |
||
78 | .then(function() { |
||
79 | document.dispatchEvent(new Event("DOMChanged")); |
||
80 | }); |
||
81 | }; |
||
82 | |||
83 | var getSubmitButton = function(form) { |
||
84 | if (form.srcElement.querySelector("[type=submit]") !== null) { |
||
85 | return form.srcElement.querySelector("[type=submit]"); |
||
86 | } |
||
87 | if (form.srcElement.getElementsByTagName("button") !== null) { |
||
88 | return form.srcElement.getElementsByTagName("button")[0]; |
||
89 | } |
||
90 | return null; |
||
91 | }; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Transform image's path (src) produce with Liip to responsive path |
||
96 | * |
||
97 | * @param {string} src |
||
98 | */ |
||
99 | export function responsiveImage(src) { |
||
100 | var screenWidth = window.innerWidth; |
||
101 | if (screenWidth <= 576) { |
||
102 | src = src.replace("/default/", "/xs/"); |
||
103 | } else if (screenWidth <= 768) { |
||
104 | src = src.replace("/default/", "/sm/"); |
||
105 | } else if (screenWidth <= 992) { |
||
106 | src = src.replace("/default/", "/md/"); |
||
107 | } else if (screenWidth <= 1200) { |
||
108 | src = src.replace("/default/", "/lg/"); |
||
109 | } else { |
||
110 | // 1200+ |
||
111 | src = src.replace("/default/", "/xl/"); |
||
112 | } |
||
113 | |||
114 | return src; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Convert elements wich contain attribute (data-href) in normal link (a href) |
||
119 | * You can use a callback function to decrypt the link (eg: rot13ToText ;-)) |
||
120 | * |
||
121 | * @param {string} attribute |
||
122 | */ |
||
123 | export async function uncloakLinks(attribute = "data-rot") { |
||
124 | var convertLink = function(element) { |
||
125 | // fix "bug" with img |
||
126 | if (element.getAttribute(attribute) === null) { |
||
324 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.